<?php

namespace liketry\controllers;

use Yii;
use yii\filters\VerbFilter;
use services\models\MemberAddress;
use common\widgets\SelectWidget;

class MemberAddressController extends CustomController
{
    //声明私有变量
    public static $member_uid;
    public static $model;

    //行为规则验证
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'add' => ['POST'],
                    'edit' => ['POST']
                ],
            ],
        ];
    }

    //验证登录 用户信息初始化


    //新增地址
    public function actionAdd()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回
        //校验

        $params = Yii::$app->request->post();
        $params['addtime'] = time();
        $params['member_uid'] = $this->bestry_uid;


        $MemberAddress = new MemberAddress();

        //判断大于10条不能添加
        $count = $MemberAddress->find()->where(['member_uid' => $this->bestry_uid])->count();

        if ($count >= 10)
            return ['data' => '', 'msg' => '收货地址不能超过十个', 'error_code' => '-401'];

        if($count >=10)
            return ['data'=>'','msg'=>'收货地址不能超过十个','error_code'=>'-401'];

        $addressInfo = $MemberAddress->addressSetAdd($params);

        return $addressInfo;

    }

    //删除地址
    public function actionDel()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回
        //地址列表
        $uid = Yii::$app->request->get('uid');

        $MemberAddress = new MemberAddress();
        $addressInfo = $MemberAddress->addressSetDel($this->bestry_uid, $uid);

        return $addressInfo;

    }

    //编辑地址
    public function actionEdit()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回
        //地址列表
        $params = Yii::$app->request->post();
        //$params['addtime'] = time();取消修改地址更改添加时间
        $params['member_uid'] = $this->bestry_uid;
        $params['uid'] = isset($params['uid']) ? $params['uid'] : 0;
        $MemberAddress = new MemberAddress();

        $addressInfo = $MemberAddress->addressSetEdit($params);

        //拼接编辑工具


        return $addressInfo;
    }

    //设置默认地址
    public function actionDefault()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回
        $MemberAddress = new MemberAddress();
        $uid = Yii::$app->request->get('uid');
        $addressInfo = $MemberAddress->addressSetDefault($this->bestry_uid, $uid);
        //返回列表数据
        return ['data' => $addressInfo, 'msg' => '当前用户的全部列表！', 'code' => '200'];
    }

    //查看全部
    public function actionConsult()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回
        $MemberAddress = new MemberAddress();
        $addressInfo = $MemberAddress->addressList($this->bestry_uid);
        return ['data'=>\yii\helpers\ArrayHelper::toArray($addressInfo, ['services\models\MemberAddress' => [
            'uid',
            's1_uid',
            's2_uid',
            's3_uid',
            'consignee',
            's1',
            's2',
            's3',
            'address',
            'zipcode',
            'cell',
            'tel1',
            'tel2',
            'tel3',
            'order_email',
            'is_default',
            'addtime',
            'express_charge' => function ($data) {
                return $data->express_charge;
            }
        ]]),'msg'=>'当前用户的全部列表！','code'=>'200'];;
        //返回列表数据
        #return ['data'=>$addressInfo,'msg'=>'当前用户的全部列表！','code'=>'200'];
    }

    //获取下拉框城市列表
    public function actionSelectData()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回
        $parent_id = Yii::$app->request->get('parent_id');
        $layer = Yii::$app->request->get('layer');

        $MemberAddress = new MemberAddress();
        $addressInfo = $MemberAddress->getAreaAddress($parent_id, $layer);

        return $addressInfo;
    }


    //获取当前地址信息
    public function actionAddressInfo()
    {
        Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;   //依照json返回
        $uid = Yii::$app->request->get('uid');
        $MemberAddress = new MemberAddress();

        $addressInfo = $MemberAddress->getMemberAddressInfo($this->bestry_uid, $uid);

        if(isset($addressInfo['code']))
            return '';

        if(count($addressInfo) <= 0)
            return '';
        //获取layer =1城市

        $layerPro = $MemberAddress->getAreaAddress(1, 1);

        $layerProS1 = SelectWidget::widget(['SelectData' => $layerPro, 'value' => $addressInfo->s1_uid, 'isOr' => 0]);

        //获取layer =2城市
        $layerCity = $MemberAddress->getAreaAddress($addressInfo->s1_uid, 2);
        $layerProS2 = SelectWidget::widget(['SelectData' => $layerCity, 'value' => $addressInfo->s2_uid, 'isOr' => 0]);


        //获取layer =3城市
        $layerArea = $MemberAddress->getAreaAddress($addressInfo->s2_uid, 3);
        $layerProS3 = SelectWidget::widget(['SelectData' => $layerArea, 'value' => $addressInfo->s3_uid, 'isOr' => 0]);

        //赋值
        $addressInfo->s1 = $layerProS1;
        $addressInfo->s2 = $layerProS2;
        $addressInfo->s3 = $layerProS3;

        return $addressInfo;

    }

    //获取当前地址信息
    public function actionPersonalCenter(){


        return $this->render('personal-center');
    }

}
